Page({
  data: {
    currentStatus: '',
    orderList: [
      {
        id: 1,
        status: 'paid',
        statusText: '待接单',
        customerName: '李先生',
        customerPhone: '13800138000',
        serviceName: '全身放松按摩',
        appointmentTime: '2023-05-20 14:00',
        address: '北京市朝阳区某某小区1号楼101室',
        price: 198,
        actions: [
          { action: 'accept', text: '接单' }
        ]
      },
      {
        id: 2,
        status: 'accepted',
        statusText: '已接单',
        customerName: '王女士',
        customerPhone: '13900139000',
        serviceName: '足底按摩',
        appointmentTime: '2023-05-18 10:00',
        address: '北京市朝阳区某某大厦201室',
        price: 128,
        actions: [
          { action: 'arrive', text: '到达' }
        ]
      }
    ],
    page: 1,
    pageSize: 10,
    noMoreData: false
  },

  onLoad: function() {
    this.loadOrderList();
  },

  loadOrderList: function() {
    const app = getApp();
    const { currentStatus, page, pageSize } = this.data;
    
    app.request({
      url: `${app.globalData.baseUrl}/technician/orders/`,
      data: {
        status: currentStatus,
        page: page,
        page_size: pageSize
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const newList = res.data.results || res.data;
          const orderList = page === 1 ? newList : [...this.data.orderList, ...newList];
          
          this.setData({
            orderList: orderList,
            noMoreData: newList.length < pageSize
          });
        }
      }
    });
  },

  switchStatus: function(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({
      currentStatus: status,
      page: 1
    });
    this.loadOrderList();
  },

  goToOrderDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/technician/orderDetail?id=${id}`
    });
  },

  handleAction: function(e) {
    const action = e.currentTarget.dataset.action;
    const orderId = e.currentTarget.dataset.id;
    
    switch(action) {
      case 'accept':
        this.acceptOrder(orderId);
        break;
      case 'arrive':
        this.arriveOrder(orderId);
        break;
      case 'start':
        this.startService(orderId);
        break;
      case 'complete':
        this.completeOrder(orderId);
        break;
    }
  },

  acceptOrder: function(orderId) {
    const app = getApp();
    
    app.request({
      url: `${app.globalData.baseUrl}/orders/${orderId}/accept/`,
      method: 'POST',
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '接单成功',
            icon: 'success'
          });
          // 重新加载订单列表
          this.loadOrderList();
        } else {
          wx.showToast({
            title: '接单失败',
            icon: 'none'
          });
        }
      }
    });
  },

  arriveOrder: function(orderId) {
    const app = getApp();
    
    app.request({
      url: `${app.globalData.baseUrl}/orders/${orderId}/arrive/`,
      method: 'POST',
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '已到达',
            icon: 'success'
          });
          // 重新加载订单列表
          this.loadOrderList();
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none'
          });
        }
      }
    });
  },

  startService: function(orderId) {
    const app = getApp();
    
    app.request({
      url: `${app.globalData.baseUrl}/orders/${orderId}/start/`,
      method: 'POST',
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '开始服务',
            icon: 'success'
          });
          // 重新加载订单列表
          this.loadOrderList();
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none'
          });
        }
      }
    });
  },

  completeOrder: function(orderId) {
    const app = getApp();
    
    app.request({
      url: `${app.globalData.baseUrl}/orders/${orderId}/complete/`,
      method: 'POST',
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '服务完成',
            icon: 'success'
          });
          // 重新加载订单列表
          this.loadOrderList();
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none'
          });
        }
      }
    });
  },

  onReachBottom: function() {
    // 上拉加载更多
    if (!this.data.noMoreData) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadOrderList();
    }
  }
})